package models;

import java.util.Collections;
import java.util.List;

import javax.cache.Cache;
import javax.cache.CacheException;
import javax.cache.CacheManager;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyOpts;
import com.googlecode.objectify.ObjectifyService;

import entities.GoogleAdsense;

public class GoogleAdsenceModel {
	private Objectify ofy;
	
	private Cache cache=null;
	
	private String prefix = "ge_";
	
	private int limit = 100;
	
	private static GoogleAdsenceModel instance = null;
	
	public static GoogleAdsenceModel getInstance() {
		if (instance == null) {
			instance = new GoogleAdsenceModel();
		}
		
		return instance;
	}
	
	private GoogleAdsenceModel() {
		try {
			ObjectifyService.register(GoogleAdsense.class);
		} catch (Exception e) {
			//e.printStackTrace();
		}
		
		/*try {
            cache = CacheManager.getInstance().getCacheFactory().createCache(Collections.emptyMap());
        } catch (CacheException e) {
           //e.printStackTrace();
        }*/
		
        ObjectifyOpts opts = new ObjectifyOpts().setSessionCache(true);
		ofy = ObjectifyService.begin(opts);
	}
	
	public void insert(GoogleAdsense adsence) {
		if (cache != null) {
			cache.put(prefix + adsence.getAdsAccount(), adsence);
		}
		
		ofy.async().put(adsence);
	}
	
	public GoogleAdsense get(String googleAdsenceId) {
		GoogleAdsense adsence = new GoogleAdsense();
		
		if (cache != null && cache.containsKey(prefix + googleAdsenceId)) {
			adsence = (GoogleAdsense) cache.get(prefix + googleAdsenceId);
		} else {
			try {
				adsence = ofy.get(new Key<GoogleAdsense>(GoogleAdsense.class, googleAdsenceId));
				if (cache != null) {
					cache.put(prefix + googleAdsenceId, adsence);
				}
			} catch (Exception e) {
				adsence = null;
			}
		}
		return adsence;
	}
	
	public void add(String googleAdsenceId, List<String> links) {
		boolean isChange = false;
		GoogleAdsense adsence = get(googleAdsenceId);
		if (adsence == null) {
			adsence = new GoogleAdsense();
			adsence.setAdsAccount(googleAdsenceId);
		}
		
		List<String> domains = adsence.getDomains();
		for (int i = 0; i < links.size(); i++) {
			if (!domains.contains(links.get(i)) && domains.size() <= limit) {
				isChange = true;
				domains.add(links.get(i));
			}
		}
		
		adsence.setDomains(domains);
		
		if (isChange) {
			if (cache != null) {
				cache.put(prefix + googleAdsenceId, adsence);
			}
			insert(adsence);
		}
	}
	
	public void insert(String googleAdsenceId, String domain) {
		boolean isChange = false;
		GoogleAdsense adsence = get(googleAdsenceId);
		if (adsence == null) {
			adsence = new GoogleAdsense();
			adsence.setAdsAccount(googleAdsenceId);
		}
		
		List<String> domains = adsence.getDomains();
		if (!domains.contains(domain) && domains.size() <= limit) {
			isChange = true;
			domains.add(domain);
		}
		
		adsence.setDomains(domains);
		
		if (isChange) {
			if (cache != null) {
				cache.put(prefix + googleAdsenceId, adsence);
			}
			insert(adsence);
		}
	}
	
	public void delete(GoogleAdsense adsence) {
		if (cache != null && cache.containsKey(prefix + adsence.getAdsAccount())) {
			cache.remove(prefix + adsence.getAdsAccount());
		}
		ofy.delete(adsence);
	}
}
